<template>
	<globalModal
		:title="title"
		:visible="visible"
		:width="400"
		:bodyStyle="bodyStyle"
		:confirmLoading="confirmLoading"
		@ok="handleOk"
		@cancel="handleCancel"
	>
		<a-spin :spinning="confirmLoading">
			<a-form :form="form" autocomplete="off" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<div class="flex flex-pack-center">
					<a-radio-group v-model="reportType" button-style="solid" @change="changeType">
						<a-radio-button value="classId"> 班级报告 </a-radio-button>
						<a-radio-button value="gradeId"> 年级报告 </a-radio-button>
					</a-radio-group>
				</div>
				<a-form-item label="选择班级" :colon="false" v-if="reportType == 'classId'">
					<a-radio-group
						style="margin-top: 8px"
						v-decorator="['classId', { rules: [{ required: true, message: '请选择班级' }] }]"
					>
						<a-row>
							<a-col
								:span="12"
								style="margin-bottom: 10px"
								v-for="(item, index) in classList"
								:key="index"
							>
								<a-radio :value="item.classId">
									{{ item.className }}
								</a-radio>
							</a-col>
						</a-row>
					</a-radio-group>
				</a-form-item>
				<a-form-item
					label="选择班级"
					:colon="false"
					help="不选班级导出全部班级报告"
					v-if="reportType == 'gradeId'"
				>
					<a-checkbox-group style="margin-top: 8px;display: block" v-decorator="['classIds']">
						<a-row>
							<a-col
								:span="12"
								style="margin-bottom: 10px"
								v-for="(item, index) in classList"
								:key="index"
							>
								<a-checkbox :value="item.classId">
									{{ item.className }}
								</a-checkbox>
							</a-col>
						</a-row>
					</a-checkbox-group>
				</a-form-item>
			</a-form>
		</a-spin>
	</globalModal>
</template>

<script>
import { sysManageapi } from '@/api/index.js';
import { ModalMixins } from '@/mixins';
export default {
	name: 'selectClassModal',
	mixins: [ModalMixins],
	data() {
		return {
			bodyStyle: {
				maxHeight: '450px',
				minHeight: '100px',
				overflow: 'auto',
			},
			labelCol: {
				xs: {
					span: 24,
				},
				sm: {
					span: 6,
				},
			},
			wrapperCol: {
				xs: {
					span: 24,
				},
				sm: {
					span: 18,
				},
			},
			classList: [], //班级数组
			operateType: 0,
			title: '刷卡绑定',
			mdl: {},
			reportType: 'classId',
		};
	},
	components: {},
	methods: {
		show(type, record) {
			this.mdl = { ...record };
			this.operateType = type;
			this.title = type == 0 ? '刷卡绑定' : '分析报告';
			this.reportType = 'classId';
			if (this.operateType != 0) {
				this.classList = [...record.sendTestClassVos].filter((item) => item.state == 1 || item.zgState == 1);
				if (this.classList && this.classList.length > 0) {
					this.$nextTick(() => {
						this.form.setFieldsValue({ classId: this.classList[0].classId });
					});
				}
			} else {
				this.getClassList();
			}
			this.visible = true;
		},
		//班级
		getClassList(deptId) {
			return this.$postAction(sysManageapi.classUrl.getAllList, {
				orgId: this.globalOrgId || '',
				deptId: deptId,
			}).then((res) => {
				if (res && res.code == 1) {
					this.classList = res.data;
				}
			});
		},
		changeType(e) {
			console.log('e', e.target.value);
			if (this.classList && this.classList.length > 0) {
				this.$nextTick(() => {
					if (this.reportType == 'classId') {
						this.form.setFieldsValue({ classId: this.classList[0].classId });
					} else {
						this.form.setFieldsValue({ classIds: this.classList.map((item) => item.classId) });
					}
				});
			}
		},
		handleOk() {
			const _this = this;
			// 触发表单验证
			this.form.validateFields((err, values) => {
				// 验证表单没错误
				if (!err) {
					/* 处理地址 */
					let parameter = {};
					let { testQuestId } = this.mdl;
					_this.confirmLoading = true;
					if (this.operateType == 0 || this.reportType == 'classId') {
						let className = this.classList.find((item) => item.classId == values.classId).className;
						this.$ls.set('TEST_INFO', { classId: values.classId, testQuestId: testQuestId });

						if (this.operateType == 0) {
							_this.$router.push({
								path: '/studentManage/bindDevice',
								query: {
									classId: values.classId,
									className,
								},
							});
						} else {
							_this.getSdTestQuestId(testQuestId, values.classId, className);
							/* this.$ls.set('secondselectKey', 'analysis');
							_this.$router.push({
								path:'/analysis/classreport',
								query:{
									testQuestId,
									classId:values.classId,
								}
							}) */
						}
					} else {
						this.downloadGradeReport(values.classIds);
					}
					this.$emit('ok');
					this.handleCancel();
				}
			});
		},

		async getSdTestQuestId(testQuestId, classId, className) {
			let _this = this;
			return this.$postAction(sysManageapi.answerUrl.getSdTestQuestId, {
				testQuestId,
				classId,
			})
				.then((res) => {
					if (res && res.code == 1) {
						_this.confirmLoading = false;
						let { sdTestQuestId, createTime } = res.data;

						this.$ls.set('secondselectKey', 'analysis');
						_this.$router.push({
							path: '/analysis/classreport',
							/* query:{
								testQuestId,
								classId,
								sdTestQuestId,
								className,
								createTime
							} */
							query: {
								testQuestId,
								sdTestQuestId,
								classId,
							},
						});
					}
				})
				.finally((res) => {
					setTimeout(() => {
						_this.confirmLoading = false;
					}, 800);
				});
		},
		//下载年级报告
		downloadGradeReport(classIds) {
			let { testQuestId, gradeId, gradeName, title } = this.mdl;
			let url = sysManageapi.exportUrl.downClassReportGK + '?testQuestId=' + testQuestId + '&gradeId=' + gradeId;
			if (classIds && classIds.length > 0) {
				url = url + '&classIds=' + classIds.join(',');
			}
			this.$postActionExport(url)
				.then((res) => {
					let BlobFile = res;
					console.log(BlobFile);
					let blobObject = new Blob([BlobFile], {
						type: 'application/vnd.ms-excel,charset=utf-8',
						//type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,charset=utf-8",
					}); //如果是字节流
					//是IE浏览器
					if (!!window.ActiveXObject || 'ActiveXObject' in window) {
						window.navigator.msSaveOrOpenBlob(blobObject, fileName);
					} else {
						//火狐谷歌都兼容
						//模板中要有一个预定义好的a标签
						var link = document.createElement('a');
						link.href = window.URL.createObjectURL(blobObject); //如果是字节流
						//link.href = BlobFile;
						link.target = '_blank';
						link.download = '年级报告：' + (gradeName || '') + '《' + title + '》' + '.xlsx';
						link.click();
						//释放内存
						//window.URL.revokeObjectURL(link.href)
					}
					this.$message.success('导出成功');
				})
				.finally((res) => {
					setTimeout(() => {
						this.confirmLoading = false;
					}, 800);
				});
		},
		handleCancel() {
			this.$emit('close');
			this.visible = false;
		},
	},
};
</script>

<style scoped></style>
